<?php


namespace App\Http\Services;


use App\Banner;
use App\Button;
use App\Classify;
use App\Enterprise;
use App\Merchant;
use App\MerchantMedicine;
use App\MerchantMedicineArea;
use App\MerchantMedicineImg;
use App\MerchantMedicineXArea;
use App\MerchantMedicineXClassify;

class ShopCopy
{
    protected $template_id      = 0;
    protected $new_merchant_id  = 0;
    protected $enterprise_id    = 0;
    /**
     * @var int
     */
    protected $copy_banner;
    /**
     * @var int
     */
    protected $copy_medicine;
    /**
     * @var int
     */
    protected $copy_button;
    /**
     * @var int
     */
    protected $copy_area;
    protected $error_message = [];
    protected $log_message   = [];

    protected $medicine_array = [];//[key=>val,old_id=>new_id]
    protected $classify_array = [];//[key=>val,old_id=>new_id]

    public function __construct($template_id,$new_merchant_id,$copy_area,$copy_banner,$copy_button,$copy_medicine)
    {
        $this->template_id  = $template_id ?? 0;
        $this->copy_area    = $copy_area ?? 0;
        $this->copy_banner  = $copy_banner ?? 0;
        $this->copy_button  = $copy_button ?? 0;
        $this->copy_medicine= $copy_medicine ?? 0;
        $this->new_merchant_id= $new_merchant_id ?? 0;
        $this->enterprise_id= Merchant::getEnterpriseId($this->template_id);
        $this->checkConfig();
    }

    /**
     * @return bool
     */
    protected function checkConfig(){
        if (!$this->template_id)
            array_push($this->error_message,'模板门店为0');
        if (!$this->new_merchant_id)
            array_push($this->error_message,'需要填充数据的门店为0');
        if (!$this->enterprise_id)
            array_push($this->error_message,'通过模板门店无法找到所属的企业,或模板门店不存在');
        if (!in_array($this->new_merchant_id,Enterprise::getEnterpriseMerchantIds($this->enterprise_id)))
            array_push($this->error_message,'填充数据的门店不属于模板门店');
        if (!Merchant::whereId($this->new_merchant_id)->first())
            array_push($this->error_message,'需要填充数据的门店不存在');
        if (count($this->error_message) > 0) {
            //TODO 复制失败,添加日志
            return false;
        }
        return true;
    }

    /**
     * @return array
     */
    protected function getErrors(){
        return $this->error_message;
    }

    /**
     * 调用这个复制
     * @return bool
     */
    public function customCopy(){
        if (count($this->getErrors()) > 0){
            //有错误，无法复制
            //TODO 写日志
            return false;
        }
        if ($this->copy_medicine){
            $medicine_num = $this->copyMedicine();
        }
        if ($this->copy_banner){
            $banner_num = $this->copyBanner();
        }
        if ($this->copy_area){
            $area_num = $this->copyArea();
        }
        if ($this->copy_button){
            $button_num = $this->copyButton();
        }
        //TODO 写复制日志

    }

    /**
     * @return int
     */
    public function copyArea(){
        $area_data      = MerchantMedicineArea::whereMerchantId($this->template_id)->get();
        $has_area       = MerchantMedicineArea::whereMerchantId($this->new_merchant_id)->get();
        if ($has_area)
            return 0;
        if (count($area_data) > 0){
            foreach ($area_data as $item){
                $area               = new MerchantMedicineArea();
                $area->merchant_id  = $this->new_merchant_id;
                $area->name         = $item->name;
                $area->desc         = $item->desc;
                $area->img_id       = $item->img_id;
                $area->sort         = $item->sort;
                $area->status       = $item->status;
                $area->save();
                $area_medicine = MerchantMedicineXArea::whereMerchantMedicineAreaId($item->id)->get();
                if (count($area_medicine) > 0){
                    foreach ($area_medicine as $value){
                        if (isset($this->medicine_array[$item->id]) && $this->medicine_array[$item->id])
                            MerchantMedicineXArea::firstOrCreate(
                            [
                                'merchant_medicine_area_id'=>$area->id,
                                'merchant_medicine_id'=>$this->medicine_array[$item->id]
                            ]
                        );
                    }
                }
            }
        }
        return count($area_data);
    }

    /**
     * @return int
     */
    public function copyBanner(){
        $banner_data    = Banner::whereMerchantId($this->template_id)->get();
        $has_banner     = Banner::whereMerchantId($this->new_merchant_id)->get();
        if ($has_banner)
            return 0;
        if (count($banner_data) > 0){
            foreach ($banner_data as $record){
                $banner = new Banner();
                $banner->merchant_id    = $this->new_merchant_id;
                $banner->img_id         = $record->img_id;
                $banner->title          = $record->title;
//                $banner->link_url       = $record->link_url;
                $banner->location       = $record->location;
                $banner->sort           = $record->sort;
                $banner->status         = $record->status;
                $banner->save();
            }
        }
        return count($banner_data);
    }

    /**
     * @return int
     */
    public function copyButton(){
        $button_data    = Button::whereMerchantId($this->template_id)->get();
        $has_button     = Button::whereMerchantId($this->new_merchant_id)->get();
        if (count($has_button) > 0)
            return 0;
        if (count($button_data) > 0){
            foreach ($button_data as $item){
                $button                 = new Button();
                $button->merchant_id    = $this->new_merchant_id;
                $button->classify_id    = $item->classify_id;
                $button->img_id         = $item->img_id;
                $button->title          = $item->title;
//                $button->link_url    = $item->link_url;
                $button->location       = $item->location;
                $button->sort           = $item->sort;
                $button->save();
            }
        }
        return count($button_data);
    }

    /**
     * @return int
     */
    public function copyClassify(){
        $father_classify_data   = Classify::whereMerchantId($this->template_id)->wherePid(0)->get();
        $son_classify_data      = Classify::whereMerchantId($this->template_id)->where('pid','>',0)->get();
        $has_classify   = Classify::whereMerchantId($this->template_id)->get();
        if ($has_classify)
            return 0;
        if (count($has_classify) > 0)
            return 0;
        if (count($father_classify_data) > 0){
            foreach ($father_classify_data as $item){
                $classify               = new Classify();
                $classify->enterprise_id= $this->enterprise_id;
                $classify->name         = $item->name;
                $classify->desc         = $item->desc;
                $classify->merchant_id  = $this->new_merchant_id;
                $classify->pid          = 0;
                $classify->type         = $item->type;
//                $classify->link_url     = $item->link_url;
                $classify->img_id       = $item->img_id;
                $classify->status       = $item->status;
                $classify->sort         = $item->sort;
                $classify->save();
                $this->classify_array[$item->id] = $classify->id;
            }
        }
        if (count($son_classify_data) > 0){
            foreach ($son_classify_data as $item){
                $classify               = new Classify();
                $classify->enterprise_id= $this->enterprise_id;
                $classify->name         = $item->name;
                $classify->desc         = $item->desc;
                $classify->merchant_id  = $this->new_merchant_id;
                $classify->pid          = $this->classify_array[$item->pid];
                $classify->type         = $item->type;
//                $classify->link_url     = $item->link_url;
                $classify->img_id       = $item->img_id;
                $classify->status       = $item->status;
                $classify->sort         = $item->sort;
                $classify->save();
                $this->classify_array[$item->id] = $classify->id;
            }
        }
        return count($father_classify_data) + count($son_classify_data);
    }

    /**
     * @return int
     */
    public function copyMedicine(){
        $has_medicine = MerchantMedicine::whereMerchantId($this->new_merchant_id)->first();
        if ($has_medicine)
            return 0;
        $medicine_data = MerchantMedicine::whereMerchantId($this->template_id)->get();
        if (count($medicine_data) > 0){
            foreach ($medicine_data as $item){
                $medicine = new MerchantMedicine();
                $medicine->upc              = $item->upc;
                $medicine->name             = $item->name ?? '';
                $medicine->merchant_id      = $this->new_merchant_id;
                $medicine->approval_number  = $item->approval_number ?? '';
                $medicine->specification    = $item->specification ?? '';
                $medicine->selling_price    = $item->selling_price ?? 0;
                $medicine->stock            = $item->stock ?? 0;
                $medicine->manufacturer     = $item->manufacturer ?? '';
                $medicine->manufacturer_address = $item->manufacturer_address ?? '';
                $medicine->manufacturer_contact = $item->manufacturer_contact ?? '';
                $medicine->electronic_regulatory_code = $item->electronic_regulatory_code ?? '';
                $medicine->usage_and_dosage = $item->usage_and_dosage ?? '';
                $medicine->indications      = $item->indications ?? '';
                $medicine->adapt_to_the_crowd = $item->adapt_to_the_crowd ?? '';
                $medicine->component        = $item->component ?? '';
                $medicine->adverse_reactions = $item->adverse_reactions ?? '';
                $medicine->contraindications = $item->contraindications ?? '';
                $medicine->precautions      = $item->precautions ?? '';
                $medicine->interaction      = $item->interaction ?? '';
                $medicine->medication_for_children = $item->medication_for_children ?? '';
                $medicine->medication_for_pregnancy_x_lactation = $item->medication_for_pregnancy_x_lactation ?? '';
                $medicine->medication_for_aged = $item->medication_for_aged ?? '';
                $medicine->properties       = $item->properties ?? '';
                $medicine->packing          = $item->packing ?? '';
                $medicine->dosage_form      = $item->dosage_form ?? '';
                $medicine->storage          = $item->storage ?? '';
                $medicine->pharmacological_action = $item->pharmacological_action ?? '';
                $medicine->expiry_date      = $item->expiry_date ?? '';
                $medicine->detail           = $item->detail ?? '';
                $medicine->is_shop          = $item->is_shop ? 1 : 0;
                $medicine->sort             = $item->sort ?? 0;
                $medicine->save();
                $this->medicine_array[$item->id] = $medicine->id;
                $medicine_classify = MerchantMedicineXClassify::whereMerchantId($this->template_id)->get();
                if (count($medicine_classify) > 0){
                    foreach ($medicine_classify as $record){
                        if (isset($this->classify_array[$record->id]) && $this->classify_array[$record->id]){
                            MerchantMedicineXClassify::firstOrCreate(
                                ['merchant_medicine_id'=> $medicine->id,'classify_id'=>$this->classify_array[$record->id]]
                            );
                        }
                    }
                }
                $img_list = MerchantMedicineImg::whereMerchantMedicineId($item->id)->get();
                if (count($img_list) > 0) {
                    foreach ($img_list as $img){
                        MerchantMedicineImg::firstOrCreate(
                            ['merchant_medicine_id'=>$medicine->id, 'img_id'=>$img->img_id,],
                            ['sort'=>$img->sort]);
                    }
                }
            }
        }
        return count($medicine_data);
    }


}